package com.tzg.iot.http;

import com.alibaba.fastjson.JSONObject;
import com.tzg.iot.http.info.UserInfo;
import com.tzg.iot.http.service.DeviceService;
import com.tzg.iot.http.service.LoginService;
import com.tzg.iot.http.service.SendDataService;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author tzg157
 */
public class ClientMain {

    public static final int PORT = 9090;
    public static final String IP = "localhost";

    public static void main(String[] args) throws IOException {
        LoginService loginService = new LoginService();
        DeviceService deviceService = new DeviceService();
        SendDataService sendDataService = new SendDataService();
        String tokenJsonStr = loginService.login(UserInfo.TENANT_USER,UserInfo.TENANT_PWD);
//        System.out.println(tokenJsonStr);

        List<Map<String, Object>> list = deviceService.deviceList(40, "");

        if(list != null && list.size() > 0) {
            Map<String, Object> device = list.get(list.size()-1);
            String deviceId = deviceService.getDeviceId(device);
            String credentialsId = deviceService.getCredentials(deviceId);
            System.out.println("== credentialsId " + credentialsId);

            Map<String,Object> attrData = new HashMap<>(16);
            attrData.put("test5","test567");
            attrData.put("test6","test2s222");
            sendDataService.sendAttributeData(credentialsId,JSONObject.toJSONString(attrData));

            Map<String,Object> telemetryData = new HashMap<>(16);
            telemetryData.put("test7","test1888");
            telemetryData.put("test8","test2777");
            sendDataService.sendTelemetryData(credentialsId,JSONObject.toJSONString(telemetryData));
        }
    }

}
